-- Prefan Access Controller
-- User Access Control, dubbed UAC

--[[ Rank List for those confused!
	Super Admin 	 	0
	Administrator 		1
	Junior Admin		2
	Moderator			3
	VIP					4
	Known				5
	Guest				6
	Minge				7
--]]

-- Variables

PAC.Levels = {}

local function AddLevel( name, disc, num, snum )

	local data = {}
		data.Name = name
		data.Disc = disc
		data.Num = num
		data.SNum = snum
		
	table.insert( PAC.Levels, data )
	
end

AddLevel( "Super Admin", "Head Honcho, runs the entire server.  Probably the owner!", 0, "superadmin" )
AddLevel( "Administrator", "Pawn to Super Admin, runs most of the server!", 1, "admin" )
AddLevel( "Junior Admin", "An Administrator in training!", 2, "jradmin" )
AddLevel( "Moderator", "Half of an Administrator, monitors players.", 3, "moderator" )
AddLevel( "VIP", "A person of high rank!  Not yet Admin.", 4, "vip" )
AddLevel( "Known", "A person known by the administrators!", 5, "known" )
AddLevel( "Guest", "A visitor to the server!", 6, "guest" )
AddLevel( "Minge", "A generally unwanted guest.", 7, "minge" )

-- Functions

if SERVER then

	function PAC.SetAccess( ply, user, rank )
	
		local plydata = PAC.LoadUserData( user:UniqueID() )
		
		if not plydata then PAC.Print( PAC_CONSOLE, "Couldn't load rank!" ) return end
		
		if not PAC.IsAllowed( ply, PAC.SetAccess ) then PAC.Print( PAC_CONSOLE, "Not a SUPER ADMIN!" ) return end
		
		PAC.SaveUserData( user, user:SteamID(), user:UniqueID(), rank )
		PAC.Print( PAC_CONSOLE, "Setting " .. user:Nick() .. " as " .. rank )
		PAC.Print( PAC_CHAT_ALL, Color( 255, 0, 0 ), user:Nick(), Color( 100, 100, 100 ), " has been given rank " .. rank .. "!" )

	end
	PAC.AddChatCommand( "rank", "PLAYER|STRING", "superadmin", PAC.SetAccess )
	
	function PAC.AddUsersOnJoin( ply, steamid, uniqueid )
				
		print( "Loading " .. ply:Nick() .. " data!", steamid, uniqueid )
		local plydata = PAC.LoadUserData( uniqueid )
		print( plydata )			
		
		if not plydata then print( "SAVING DATA!" ) PAC.SaveUserData( ply, steamid, uniqueid, "guest" ) end
	
	end
	hook.Add( "PlayerAuthed", "PAC_AddUsersOnJoin", PAC.AddUsersOnJoin )

end

function PAC.IsAllowed( ply, func )

	local rank = PAC.GetRank( ply )
	
	if rank == "superadmin" then return true end
	
	return false
	
end

function PAC.GetRank( ply )

	local plydata = PAC.LoadUserData( ply:UniqueID() )
	return plydata
	
end

function PAC.GetRankNumber( rank )

	for k,v in pairs( PAC.Levels ) do
	
		if v.SNum == rank or v.Name == rank then return v.Num end
		
	end
	
	return nil
	
end
	
